﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MineDiscover.Forms;
using MineDiscover.Utils;

namespace MineDiscover
{
    public partial class MainForm : Form
    {
        //Main Form Manage MapPainter
        private MapPainter mapPainter;
        private NewGameForm newGameForm;

        public MainForm()
        {
            InitializeComponent();
        }

        private void NewGameMenuItem_Click(object sender, EventArgs e)
        {
            newGameForm = new NewGameForm();
            newGameForm.NewGameClick += new EventHandler<DifficultlyEventArgs>(newGameForm_NewGameClick);
            newGameForm.ShowDialog();
        }

        private void newGameForm_NewGameClick(object sender, DifficultlyEventArgs e)
        {
            newGameForm.Close();
            GameMineDiscover.GetInstance().InitalizeGame(e.Difficluty);
            MineCountControl.MineFlag = GameMineDiscover.GetInstance().CountFlagInGame().ToString();
            mapPainter = new MapPainter(GameMineDiscover.GetInstance().MapMine, MapPanel);
            mapPainter.CreateVsPaint();
            
            // Remove
            newGameForm.NewGameClick -= new EventHandler<DifficultlyEventArgs>(newGameForm_NewGameClick);

            // Create Event
            MapPanel.MouseClick -= MapPanel_MouseClick;
            MapPanel.MouseMove -= MapPanel_MouseMove;
            MapPanel.MouseMove += new MouseEventHandler(MapPanel_MouseMove);
            MapPanel.MouseClick += new MouseEventHandler(MapPanel_MouseClick);
        }

        private void MapPanel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (!GameMineDiscover.GetInstance().IsStarted)
                {
                    GameMineDiscover.GetInstance().IsStarted = true;
                    GameMineDiscover.GetInstance().StartGame(e.Location, MapPanel.Size);
                    // Count Time
                    TimeCountControl.BeginCount();
                }

                if (GameMineDiscover.GetInstance().ValidateClickMine(e.Location, MapPanel.Size))
                {
                    MapPanel.MouseClick -= MapPanel_MouseClick;
                    MapPanel.MouseMove -= MapPanel_MouseMove;
                    mapPainter.EndGame();

                    MessageBox.Show("You Lost :(");
                }
                else
                {
                    mapPainter.OnLeftClick();

                    if (GameMineDiscover.GetInstance().Validate2Win(e.Location, MapPanel.Size))
                    {
                        MapPanel.MouseClick -= MapPanel_MouseClick;
                        MapPanel.MouseMove -= MapPanel_MouseMove;
                        mapPainter.WinGame();

                        MessageBox.Show("You Won :)");
                    }
                }
            }

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                mapPainter.OnRightClick();

                MineCountControl.MineFlag = GameMineDiscover.GetInstance().CountFlagInGame().ToString();
            }
        }

        private void MapPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (mapPainter != null)
            {
                mapPainter.MouseOverMap(e.Location);
            }
        }

        private void RestartGameMenuItem_Click(object sender, EventArgs e)
        {
            mapPainter = new MapPainter(GameMineDiscover.GetInstance().MapMine, MapPanel);
            mapPainter.CreateVsPaint();
        }
    }
}